﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using DotNetNuke.Tests.UI.WatiN.Common;
using System.Configuration;
using System.Web.Configuration;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.IO;
using Gallio.Framework.Pattern;
using WatiN.Core;

namespace DotNetNuke.Tests.UI.WatiN.SiteTests
{
    public class STBase : TestBase
    {
        protected WatiNBase baseWatinObject;
        protected IE ieInstance;


        protected virtual string ApplicationName
        {
            get
            {
                string _ApplicationName = null;
                if (IsUpgrade)
                {
                    _ApplicationName = "DNN_" + STContext.UpgradeVersion.ToString(3).Replace(".", "");
                }
                else
                {
                    _ApplicationName = "DNN_" + TestVersion.Replace(".", "") + "_" + SiteType;
                }
                return _ApplicationName;
            }
        }

        protected STContext STContext
        {
            get
            {
                string version = null;
                if (TestEnvironment.InstallorUpgrade.Equals("Upgrade"))
                {
                    version = TestEnvironment.UpgradeSitePackage;
                }
                STContext context = new STContext(version, SiteType);
                return context;
            }
        }

        protected string DatabaseName
        {
            get
            {
                string _DatabaseName = "DNN_" + TestVersion.Replace(".", "") + "_" + SiteType;
                return _DatabaseName;

            }
        }
        
        protected string DatabaseServer
        {
            get { return TestEnvironment.DatabaseServer; }
        }

        protected string DatabasePath
        {
            get
            {
                string physicalPath = TestEnvironment.DatabasePath;
                physicalPath = Path.Combine(physicalPath, ApplicationName);
                if (IsUpgrade)
                {
                    physicalPath = String.Format("{0}_{1}", physicalPath, SiteType);
                }
                else
                {
                    physicalPath = Path.Combine(physicalPath, SiteType);
                }
                return physicalPath;
            }
        }

        protected string CapturePath
        {
            get
            {
                string physicalPath = Path.Combine(TestEnvironment.ScreenCapturePath, "ST");
                physicalPath = Path.Combine(physicalPath, ApplicationName);
                return (!IsUpgrade) ? physicalPath : String.Format("{0}_{1}", physicalPath, SiteType);
            }
        }

       protected bool IsUpgrade
        {
            get
            {
                if (TestEnvironment.InstallorUpgrade.Equals("Install"))
                {
                    return false;
                }
                else
                {
                    return true;
                }

            }
        }

        protected string PhysicalPath
        {
            get
            {
                string physicalPath = TestEnvironment.WebsitePath;
                physicalPath = Path.Combine(physicalPath, ApplicationName);
                //return (!IsUpgrade) ? physicalPath : String.Format("{0}_{1}", physicalPath, SiteType);
                return physicalPath;
            }
        }

        protected string ForumPackagePath
        {
            get
            {
                return Path.Combine(TestEnvironment.TestFilesPath, TestEnvironment.ForumPackageName);
            }
        }

        protected override string SiteURL
        {
            get
            {
                return String.Format("http://localhost/{0}", ApplicationName);
            }
        }

        protected virtual string SiteType
        {
            get { return TestEnvironment.SiteType; }
        }



        protected Version UpgradeVersion
        {
            get { return (STContext == null) ? null : STContext.UpgradeVersion; }
        }

        #region Private Helper Methods

        protected void SetConnectionString(string applicationPath, string databaseName, string userName, string password)
        {
            string connString = String.Format("Server={0};Database={1};uid={2};pwd={3};",
                                                                TestEnvironment.DatabaseServer,
                                                                databaseName,
                                                                userName,
                                                                password);

            Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/" + applicationPath);

            //Setup Connection String
            ConnectionStringsSection connectionStringsSection = configuration.GetSection("connectionStrings") as ConnectionStringsSection;
            ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings;
            ConnectionStringSettings connectionString = connectionStrings["SiteSqlServer"];
            connectionString.ConnectionString = connString;

            AppSettingsSection appSettingsSection = configuration.GetSection("appSettings") as AppSettingsSection; ;
            //appSettingsSection.Settings["SiteSqlServer"].Value = connString;

            configuration.Save();
        }

        protected void SetUpTest()
        {
            //Setup the Website             
            SetUpWebSite(PhysicalPath, TestUsers.DBUser.UserName, TestUsers.DBUser.Password);

            //Attach the Database
            DatabaseManager.DropDatabase(DatabaseName);
            DatabaseManager.CopyAndAttachDatabase(TestContext, DatabasePath, DatabaseName);

            // Open a new Internet Explorer window and navigate to the website
            ieInstance = WatiNUtil.OpenIEInstance(SiteURL, SilentMode, IETimeOut, AutoCloseIE, AutoMovePointer);
            baseWatinObject = new WatiNBase(ieInstance, SiteURL, DatabaseName);
        }

        protected void SetUpWebSite(string physicalPath, string userName, string password)
        {
            IISManager.RecycleApplicationPool(ApplicationName);
            IISManager.CreateIISApplication(ApplicationName, physicalPath);

            //Set web.config connection settings
            SetConnectionString(ApplicationName, DatabaseName, userName, password);
        }

        protected void TearDownTest()
        {
            //Make sure user is logged out
            ieInstance.GoTo(SiteURL + "/LogOff.aspx");

            //Shut down Ie
            WatiNUtil.CloseIEInstance(ieInstance);

            //Recycle IIS App Pool
            IISManager.RecycleApplicationPool(ApplicationName);

            //Drop Database
            DatabaseManager.DropDatabase(DatabaseName);
        }


        #endregion

    }

}
